/**
 * 材料
 * Created by douxc on 2015/4/21.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('FeedAddCtrl', ['$scope', '$routeParams', 'MHService', '$filter',
      function ($scope, $routeParams, MHService, $filter) {
        $scope.showSubmit = ($routeParams['flag'] === 'new');
        /**
         * 监听输入框
         */
        $scope.$watch('search', function () {
          //name变化时
          if ($scope.search && $scope.search.length > 0) {
            MHService.postReq({url: MHService.URL.fees_search + '?key=' + $scope.search}, function (data) {
              $scope.feeds = data['datas'];
            });
          }
        });
        /**
         * 选中的材料
         * @param feed
         */
        $scope.feedSlected = function (feed) {
          $scope.selected = angular.copy(feed);
          $scope.feeds = undefined;
          $scope.search = undefined;
        };
        /**
         * 保存选中的材料
         * @param selected
         */
        $scope.saveFeed = function (selected) {
          if ($scope.intime && selected.num) {
            var _time = $filter('date')($scope.intime, 'yyyy-MM-dd');
            var submit = [selected[0], selected.num, _time].join(','),//最终提交的数据
              showData = {id: selected[0], name: selected[1], time: selected[7]};//展示在材料列表的数据
            var _value = MHService.globalData(false, {key: 'feedList'});//取出已有的材料
            if (_value) {
              //已有材料
              _value.push({submit: submit, showData: showData});
              MHService.globalData(true, {key: 'feedList', value: _value});
            } else {
              //没有值，直接保存
              MHService.globalData(true, {key: 'feedList', value: [{submit: submit, showData: showData}]});
            }
            MHService.Toast('添加成功');
            history.go(-1);
          } else {
            MHService.Toast('请检查数量和入场时间');
          }
        };
      }]);
  };
});
